package com.derbysoft.synxisadapter.action.settings.hotel;

import com.derbysoft.synxisadapter.action.BaseAdapterAction;
import com.derbysoft.synxisadapter.commons.model.Hotel;
import com.derbysoft.synxisadapter.commons.repository.HotelRepository;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

@Results(value = {
    @Result(name = Action.SUCCESS, location = "/page/settings/hotel/Edit.ftl")
})
public class EditAction extends BaseAdapterAction {

    @Autowired
    private HotelRepository hotelRepository;

    private String passport;
    private String name;
    private String id;
    private String providerPassport;
    private String providerName;
    private Hotel.Status status;

    public String execute() {
        Hotel hotel = hotelRepository.findById(id);
        if (hotel != null) {
            id = hotel.getId();
            passport = hotel.getPassport();
            name = hotel.getName();
            providerPassport = hotel.getProviderPassport();
            providerName = hotel.getProviderName();
            status = hotel.getStatus();
        }
        return SUCCESS;
    }

    public String ajaxSubmit() {
        try {
            hotelRepository.update(id, passport, name, providerPassport, providerName, status);
            return populateSucceededJsonResult(null);
        } catch (Exception e) {
            return populateFailedJsonResult(e);
        }
    }

    public String getPassport() {
        return passport;
    }

    public void setPassport(String passport) {
        this.passport = passport;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProviderPassport() {
        return providerPassport;
    }

    public void setProviderPassport(String providerPassport) {
        this.providerPassport = providerPassport;
    }

    public String getProviderName() {
        return providerName;
    }

    public void setProviderName(String providerName) {
        this.providerName = providerName;
    }

    public Hotel.Status getStatus() {
        return status;
    }

    public void setStatus(Hotel.Status status) {
        this.status = status;
    }
}